package no.ntnu.item.ttm4115.termproject.teacheragent.gui;


import java.util.Vector;

import javax.swing.DefaultListModel;

import com.thoughtworks.xstream.XStream;

import no.ntnu.item.ttm4115.termproject.CreateNewPlayerFromGUI;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;
public class NewPlayer extends javax.swing.JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	TeacherGUI gui;
    /** Creates new form newPlayer */
    public NewPlayer(TeacherGUI gui, ActorSM asm) {
    	gui.setStartPanel(this);
    	this.gui = gui;
    	this.asm = asm;
        initComponents();
         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.gui.getContentPane());
        this.gui.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(this, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
   
    private void initComponents() {

        headerLabel = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        firstNameTextField = new javax.swing.JTextField();
        surNameTextField = new javax.swing.JTextField();
        surNameLabel = new javax.swing.JLabel();
        userNameLabel = new javax.swing.JLabel();
        userNameTextField = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        groupList = new javax.swing.JList();
        addPlayerToGroup = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        playerGroupList = new javax.swing.JList();
        groupsLabel = new javax.swing.JLabel();
        playersGroupsLabel = new javax.swing.JLabel();
        playerRights = new javax.swing.JLabel();
        imLabel = new javax.swing.JLabel();
        imCheckBox = new javax.swing.JCheckBox();
        chatLabel = new javax.swing.JLabel();
        chatCheckBox = new javax.swing.JCheckBox();
        discussionsLabel = new javax.swing.JLabel();
        discussionsCheckBox = new javax.swing.JCheckBox();
        addPlayerButton = new javax.swing.JButton();
        listPlayerGroupModel = new javax.swing.DefaultListModel();
        listGroupModel = new javax.swing.DefaultListModel();

        headerLabel.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        headerLabel.setText("Add New Player");

        firstNameLabel.setText("Firstname");

      

        surNameLabel.setText("Surname");

        userNameLabel.setText("Username");
        fillGroupList(gui);
        groupList.setModel(getListGroupModel());

        jScrollPane1.setViewportView(groupList);

        addPlayerToGroup.setText("Add Group(s)");
        addPlayerToGroup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addPlayerToGroupActionPerformed(evt);
            }
        });
        
        playerGroupList.setModel(getListPlayerGroupModel());
        jScrollPane2.setViewportView(playerGroupList);

        groupsLabel.setText("Groups");

        playersGroupsLabel.setText("Players Groups");

        playerRights.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        playerRights.setText("PlayerRights");

        imLabel.setText("Instant Messaging");

        imCheckBox.setSelected(true);

        chatLabel.setText("Chat");

        chatCheckBox.setSelected(true);

        discussionsLabel.setText("Discussions");

        discussionsCheckBox.setSelected(true);

        addPlayerButton.setText("Add Player");
        addPlayerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	addPlayerButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerLabel)
                .addContainerGap(368, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(firstNameLabel)
                                    .addComponent(surNameLabel)
                                    .addComponent(userNameLabel))
                                .addGap(24, 24, 24)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(userNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                                    .addComponent(surNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                                    .addComponent(firstNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))
                                .addGap(22, 22, 22))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(playerRights)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(chatLabel)
                                .addComponent(imLabel))
                            .addGap(19, 19, 19)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(chatCheckBox)
                                .addComponent(imCheckBox)
                                .addComponent(discussionsCheckBox))
                            .addGap(42, 42, 42)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(discussionsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(playersGroupsLabel)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(groupsLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))
                    .addComponent(addPlayerToGroup, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(addPlayerButton)
                .addContainerGap(379, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(headerLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(firstNameLabel)
                        .addComponent(firstNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(groupsLabel)
                        .addComponent(playersGroupsLabel)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addPlayerToGroup)
                        .addGap(82, 82, 82))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(surNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(surNameLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(userNameLabel))
                        .addGap(20, 20, 20)
                        .addComponent(playerRights)
                        .addGap(6, 6, 6)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(imCheckBox)
                            .addComponent(imLabel))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(9, 9, 9)
                                .addComponent(chatLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(discussionsLabel))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(23, 23, 23)
                                .addComponent(discussionsCheckBox)))
                        .addGap(18, 18, 18)
                        .addComponent(addPlayerButton)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addGap(189, 189, 189)
                .addComponent(chatCheckBox)
                .addContainerGap(90, Short.MAX_VALUE))
        );
    }// </editor-fold>
    private void resetScheme(){
    	firstNameTextField.setText("");
    	surNameTextField.setText("");
    	userNameTextField.setText("");
    	listPlayerGroupModel.removeAllElements();
    	playerGroupList.repaint();
    }
    
   	private void addPlayerToGroupActionPerformed(java.awt.event.ActionEvent evt) {
		Object[] groupsToBeAdded =  groupList.getSelectedValues();
		fillGroupPlayerList(groupsToBeAdded);
	}


	private void addPlayerButtonActionPerformed(java.awt.event.ActionEvent evt) {
		Vector<String> playerGroupsVector = new Vector<String>();
		for(int i=0; i<playerGroupList.getModel().getSize();i++){
			playerGroupsVector.add((String) playerGroupList.getModel().getElementAt(i));
		}
		Player newPlayer = new Player();
		newPlayer.setChat(chatCheckBox.isSelected());
		newPlayer.setDiscussion(discussionsCheckBox.isSelected());
		newPlayer.setIm(imCheckBox.isSelected());
		newPlayer.setUserName(userNameTextField.getText());
		newPlayer.setGroups(mergeStringAndGroup(playerGroupsVector));
		
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String player = xstream.toXML(newPlayer);

		asm.sendMessage(new CreateNewPlayerFromGUI(player),new ActorAddress("/classManager","ClassManager"));
		resetScheme();
		gui.getPlayers().add(newPlayer);
		System.out.println("New player sendt til ClassManager");
	}
	private DefaultListModel getListGroupModel(){
		return listGroupModel;
	}
	private DefaultListModel getListPlayerGroupModel(){
		return listPlayerGroupModel;
	}
	private void fillGroupList(TeacherGUI gui){
		listGroupModel.removeAllElements();
		for(int i=0; i< gui.getGroups().size();i++){
			String player = gui.getGroups().get(i).getGroupName();
			listGroupModel.addElement(player);
    	}
	}
	private void fillGroupPlayerList(Object[] groups){
		if(groups != null){
			for(int i=0; i<groups.length;i++){
				if(checkPlayerGroupList((String) groups[i])){
					listPlayerGroupModel.addElement((groups[i]));
				}
			}
		}
	}
	
	private boolean checkPlayerGroupList(String group){
		boolean booleanValue = true;
		for(int i=0; i<playerGroupList.getModel().getSize();i++){
			if(playerGroupList.getModel().getElementAt(i)==group){
				booleanValue = false;
				break;
			}
		}
		return booleanValue;
	}
	private Vector<Group> mergeStringAndGroup(Vector<String> stringVector){
		Vector<Group> finalGroupVector = new Vector<Group>();
		for (int i = 0; i < stringVector.size(); i++) {
			for (int j = 0; j < gui.getGroups().size(); j++) {
				if(stringVector.elementAt(i).toString().equals(gui.getGroups().elementAt(j).getGroupName().toString())){
					finalGroupVector.add(gui.getGroups().elementAt(j));
				}
			}
		}	
		return finalGroupVector; 
	}
	

    // Variables declaration - do not modify
    private javax.swing.JButton addPlayerToGroup;
    private javax.swing.JCheckBox chatCheckBox;
    private javax.swing.JLabel chatLabel;
    private javax.swing.JCheckBox discussionsCheckBox;
    private javax.swing.JLabel discussionsLabel;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JTextField firstNameTextField;
    private javax.swing.JList groupList;
    private javax.swing.JLabel groupsLabel;
    private javax.swing.JLabel headerLabel;
    private javax.swing.JCheckBox imCheckBox;
    private javax.swing.JLabel imLabel;
    private javax.swing.JButton addPlayerButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JList playerGroupList;
    private javax.swing.JLabel playerRights;
    private javax.swing.JLabel playersGroupsLabel;
    private javax.swing.JLabel surNameLabel;
    private javax.swing.JTextField surNameTextField;
    private javax.swing.JLabel userNameLabel;
    private javax.swing.JTextField userNameTextField;
    private javax.swing.DefaultListModel listPlayerGroupModel;
    private javax.swing.DefaultListModel listGroupModel;
    private ActorSM asm;
    // End of variables declaration

}
